package com.service;

import com.qqcommon.Message;
import com.qqcommon.MessageType;
import com.qqcommon.User;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;

public class QQServer {
    private ServerSocket serverSocket = null;
    private static ConcurrentHashMap<String, User> validUsers = new ConcurrentHashMap<>();
    private static ConcurrentHashMap<String, ArrayList<Message>> offLineDb = new ConcurrentHashMap<>();

    static {
        validUsers.put("100", new User("100", "123456"));
        validUsers.put("200", new User("200", "123456"));
        validUsers.put("300", new User("300", "123456"));
        validUsers.put("紫霞仙子", new User("紫霞仙子", "123456"));
        validUsers.put("至尊宝", new User("至尊宝", "123456"));
        validUsers.put("菩提老祖", new User("菩提老祖", "123456"));
    }

    public boolean checkUser(String userId, String pwd) {
        User user = validUsers.get(userId);
        if (user == null) return false;
        if (!pwd.equals(user.getPwd())) return false;
        return true;
    }

    public QQServer() {
        System.out.println("服务器在9999端口监听");
        try {
            serverSocket = new ServerSocket(9999);
            new Thread(new SendNewsToAllService()).start();
            while (true) {
                Socket socket = serverSocket.accept();
                ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
                ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
                User user = (User) objectInputStream.readObject();
                Message message = new Message();
                if (checkUser(user.getUserId(), user.getPwd())) {
                    System.out.println("用户" + user.getUserId() + "已登录");
                    message.setMesType(MessageType.MESSAGE_LOGIN_SUCCEED);
                    objectOutputStream.writeObject(message);
                    ServerConnectClientThread serverConnectClientThread = new ServerConnectClientThread(user.getUserId(), socket);
                    serverConnectClientThread.start();
                    ManageServerConnectClientThread.addServerConnectClientThread(user.getUserId(), serverConnectClientThread);

                    //发送离线消息
                    if (offLineDb.get(user.getUserId()) != null) {
                        ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                        Message message1 = new Message();
                        message1.setMesType(MessageType.MESSAGE_OFFLINE_MES);
                        message1.setSendTime(new Date().toString());
                        message1.setOffLine(offLineDb.get(user.getUserId()));
                        oos.writeObject(message1);
                        offLineDb.remove(user.getUserId());
                    }
                } else {
                    System.out.println("登陆失败user:" + user.getUserId() + " pwd:" + user.getPwd());
                    message.setMesType(MessageType.MESSAGE_LOGIN_FAIL);
                    objectOutputStream.writeObject(message);
                    socket.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void addOffLineMes(Message message) {
        ArrayList<Message> messages = offLineDb.get(message.getGetter());
        if (messages != null)
            messages.add(message);
        else {
            ArrayList<Message> off = new ArrayList<>();
            off.add(message);
            offLineDb.put(message.getGetter(), off);
        }
    }
}
